﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DalMgr
{
    public class ErrLog
    {
        /// <summary>
        /// 日志文件夹名称
        /// </summary>
        private const string LOG_DIR = "Log\\";
        /// <summary>
        /// 定义一个静态的类，保证该类的唯一性
        /// </summary>
        private static ErrLog m_UniqueCSftLog;
        /// <summary>
        /// 日志文件夹
        /// </summary>
        private string m_LogFolder;
        /// <summary>
        /// 日志文件
        /// </summary>
        private string m_LogFileName;
        /// <summary>
        /// 私有构造函数，保证外界不能创建改类
        /// </summary>
        private ErrLog()
        {
            CreateLogFolder();
            DeleteOldLogFile();
            m_LogFileName = m_LogFolder + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
        }
        /// <summary>
        /// 获取该类的实例
        /// </summary>
        /// <returns>日志类实例</returns>
        public static ErrLog GetCSftLog()
        {
            if (m_UniqueCSftLog == null)
            {
                m_UniqueCSftLog = new ErrLog();
            }
            return m_UniqueCSftLog;
        }
        /// <summary>
        /// 如果日志文件夹不存在则创建
        /// </summary>
        private void CreateLogFolder()
        {
            m_LogFolder = AppDomain.CurrentDomain.BaseDirectory + LOG_DIR;
            if (!Directory.Exists(m_LogFolder))
            {
                Directory.CreateDirectory(m_LogFolder);
            }
        }
        /// <summary>
        /// 删除超过30天的日志文件
        /// </summary>
        private void DeleteOldLogFile()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(m_LogFolder);
            TimeSpan TS_Pase;
            foreach (FileInfo file in dirInfo.GetFiles())
            {
                TS_Pase = DateTime.Now - file.CreationTime;
                if (TS_Pase.Days>15)
                {
                    file.Delete();
                }
            }
            dirInfo = null;
        }
        /// <summary>
        /// 写入日志
        /// </summary>
        /// <param name="_Log"></param>
        public void WriteLog(string _Log)
        {
            using (FileStream FS = new FileStream(m_LogFileName, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(FS))
                {
                    sw.WriteLine(_Log);
                    sw.Close();
                    FS.Close();
                }
            }
        }
        /// <summary>                                                                         
        /// 生成自定义异常消息                                                                
        /// </summary>                                                                        
        /// <param name="ex">异常对象</param>                                                 
        /// <param name="backStr">备用异常消息：当ex为null时有效</param>                      
        /// <returns>异常字符串文本</returns>                                                 
        public string GetExceptionMsg(Exception ex, string backStr)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************异常文本****************************");
            sb.AppendLine("【出现时间】：" + DateTime.Now.ToString());
            if (ex != null)
            {
                sb.AppendLine("【异常类型】：" + ex.GetType().Name);
                sb.AppendLine("【异常信息】：" + ex.Message);
                sb.AppendLine("【堆栈调用】：" + ex.StackTrace);
            }
            else
            {
                sb.AppendLine("【未处理异常】：" + backStr);
            }
            sb.AppendLine("***************************************************************");
            return sb.ToString();
        }
    }
}
